﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Threading;
using System.Windows;
using DataBase;
using DataBase.DBmodels;
using DataBase.Utils;
using GreenXi.Help.Curve;
using GreenXi.Pages.Power;
using Microsoft.EntityFrameworkCore;
using Org.BouncyCastle.Ocsp;
using Prism.Commands;
using Prism.Mvvm;
using Region = DataBase.Region;

namespace GreenXi.ViewModel
{
    public class RealTimeDataViewModel: BindableBase
    {
        public static RealTimeData context;
        public DelegateCommand<object> SelectFilter { get; set; }
        private ObservableCollection<CraftView> _CraftList;
        public ObservableCollection<CraftView> CraftList
        {
            get => _CraftList;
            set
            {
                SetProperty(ref _CraftList, value);
            }
        }
        public class Item
        {
            public string DisplayName { get; set; }
            public int Value { get; set; }
        }
        public ObservableCollection<Item> Items { get; set; } = new ObservableCollection<Item>
{
    new Item { DisplayName = "电表", Value = 1 },
    new Item { DisplayName = "虚拟表", Value = 3 }
};

        public DelegateCommand<object> ShowColumn { get; set; }

        private ObservableCollection<Region> _RegionList;
        public ObservableCollection<Region> RegionList
        {
            get => _RegionList;
            set
            {
                SetProperty(ref _RegionList, value);
            }
        }

        private int? _CraftId;

        public int? CraftId
        {
            get => _CraftId;
            set { SetProperty(ref _CraftId, value); }
        }
        private int? _Type=1;

        public int? Type
        {
            get => _Type;
            set { SetProperty(ref _Type, value); }
        }

        private CraftView _SelectCraft;
        public CraftView SelectCraft
        {
            get => _SelectCraft;
            set
            {
                SetProperty(ref _SelectCraft, value);
            }
        }
        public RealTimeDataViewModel(RealTimeData _context) {
            context = _context;
            CraftId = GlobalVariables.CylinderNo;
            if(CraftId!=null&& CraftId != 0)
            {
                CraftList = new(GlobalVariables.CraftViewList.Where(x => x.Type != 2&& x.CraftId == CraftId&&x.Type==Type));
            }
            else
            {
                CraftList = new(GlobalVariables.CraftViewList.Where(x => x.Type != 2 && x.Type == Type));
            }

            ShowColumn = new DelegateCommand<object>((p) => { ShowColumn_Click(p); });//编辑栏目

            using MyDbContext myDbContext = new();
            RegionList = new(myDbContext.GetRegion());
            RegionList.Insert(0, new DataBase.Region() { Name = "全部" });
            SelectFilter = new DelegateCommand<object>((p) => { SelectFilter_Click(p); });

            if (SelectCraft != null)
            {
                SmoothChart.Init2(context.zedGraphControl1, SelectCraft);
            }
            InitializeTimer();
        }

        private void SelectFilter_Click(object param)
        {
            using MyDbContext myDbContext = new();
            if (CraftId != null && CraftId != 0)
            {
                CraftList = new(GlobalVariables.CraftViewList.Where(x => x.CraftId == CraftId && x.Type == Type));
            }
            else
            {
                CraftList = new(GlobalVariables.CraftViewList.Where(x => x.Type != 2 && x.Type == Type));
            }

        }

        private void ShowColumn_Click(Object param)
        {

            var paramss = param as CraftView;
            SelectCraft = paramss;
            SmoothChart.Init2(context.zedGraphControl1, SelectCraft);
     
        }


        private System.Timers.Timer _timer;

        private void InitializeTimer()
        {
            _timer = new System.Timers.Timer(5000); // 设置间隔时间（毫秒）
            _timer.Elapsed += Timer_Elapsed;
            _timer.AutoReset = true; // 是否重复执行
            _timer.Start();
        }

        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (CraftId != null && CraftId != 0)
            {
                CraftList = new(GlobalVariables.CraftViewList.Where(x => x.CraftId == CraftId && x.Type == Type));
            }
            else
            {
                CraftList = new(GlobalVariables.CraftViewList.Where(x => x.Type != 2 && x.Type == Type));
            }
            // 注意：此处运行在非 UI 线程，更新 UI 需通过 Dispatcher
            context.zedGraphControl1.AxisChange();
        }

        // 停止定时器（可选）
        public void StopTimerButton_Click()
        {
            if (_timer != null)
            {
                _timer.Stop();
            }
        }

    }
}
